﻿using MyClassLibrary;
using MyDB;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyProgram
{
    public partial class FrmDrugSchedule : Form
    {
        public FrmDrugSchedule()
        {
            InitializeComponent();
        }
        ClsDrug clsdrug = new ClsDrug();
        global::MyDB.MedicareDataClassesDataContext DB = new MyDB.MedicareDataClassesDataContext();
        private void FrmDrugSchedule_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += FrmDrugSchedule_KeyDown;
            toolStripComboBox1.SelectedIndex = 0;
            dataGridView1.DataSource = clsdrug.GetData();
        }

        void FrmDrugSchedule_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode== Keys.Enter)
            {
                btn_Search_Click( sender,e);
            }
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = clsdrug.Search(toolStripComboBox1.Text, toolStripTextBox2.Text);
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            ClsRecord.Record(sender, this.Name);
            int id = (int)(dataGridView1.SelectedRows[0].Cells[0].Value);
            clsdrug.DeleteSchedule(id);
            dataGridView1.DataSource = clsdrug.GetData();

        }
        Random randomNum = new Random();
        private void toolStripButton1_Click(object sender, EventArgs e)
        {

            int pID = Properties.Settings.Default.掛號ID % DB.Patients.Count() + 1;
            int rID=Properties.Settings.Default.掛號ID;
            int random = randomNum.Next(1, 4);
            DB.RegisterInformations.InsertOnSubmit(new MyDB.RegisterInformation { 掛號資料ID = rID, 病患ID = pID, 診次ID = random, 醫療人員ID = 4, });
            DB.Diagnosis.InsertOnSubmit(new MyDB.Diagnosi { 醫囑資料ID = rID + 5, 掛號資料ID = rID, 提醒天數ID = random, 診斷 = "骨質疏鬆", 護理師ID = 2, 諮詢師ID = 3, 醫生ID = 4 });
            DB.DrugSchedules.InsertOnSubmit(new MyDB.DrugSchedule { 醫囑資料ID = rID +5,  藥物ID = random,  用藥頻率ID = randomNum.Next(5,40), 起始日期 = DateTime.Now.AddDays(randomNum.Next(-99,0)), 結束日期 = DateTime.Now.AddDays(randomNum.Next(0,99)), 備註 = "test", 結案原因 = "尚未結案", 結案=false });
            
            DB.SubmitChanges();
            var q = from p in DB.Patients
                    where p.病患ID == pID
                    select p.姓名;
            string[] x = { "" };
            foreach (var a in q)
            {
                x[0] = a;
            }
            MessageBox.Show(string.Format("新增了一筆就診資料 \n掛號ID : {0} \n病患姓名 : {1}",Properties.Settings.Default.掛號ID,x[0]));
            Properties.Settings.Default.掛號ID += 1;
            Properties.Settings.Default.Save();
            dataGridView1.DataSource = clsdrug.GetData();

        
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            int index=dataGridView1.Rows.Count-1;
            FrmDrugScheduleNew f = new FrmDrugScheduleNew();

            if (f.ShowDialog()==DialogResult.Yes)
            {
                dataGridView1.DataSource = clsdrug.GetData();

                dataGridView1.Rows[index].Selected = true;
            }          

            
        }

        private void btn_Edit_Click(object sender, EventArgs e)
        {
            ClsDrug.editID = (int)(dataGridView1.SelectedRows[0].Cells[0].Value);
            int index=dataGridView1.SelectedRows[0].Index;
            FrmDrugEdit f = new FrmDrugEdit();

            f.ShowDialog();
            
            btn_Search_Click(sender, e);
            dataGridView1.Rows[index].Selected=true;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
